package edu.mama.college.controller.admin;

import edu.mama.college.model.*;
import edu.mama.college.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminCourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/course/list")
    public JsonResult CourseList(@RequestParam("page") int page,
                                 @RequestParam("size") int size,
                                 @RequestParam(value="name", required = false) String name,
                                 @RequestParam(value="teacher", required = false) String teacher
                                 ){
        Page<Course> courseList = courseService.courseList(page,size,name,teacher);
        return JsonResult.success(courseList);
    }

    /**
     * 发布课程
     * @param course
     * @return
     */
    @PostMapping("/course/create")
    public JsonResult createCourse( @RequestBody Course course){
      try{
          courseService.save(course,0);
          return JsonResult.success();
      }catch(Exception e){
          return JsonResult.fail(e.getMessage());
      }
    }

    /**
     * 修改
     * @param course
     * @param id
     * @return
     */
    @PutMapping("/course/{id}")
    public JsonResult updateCourse(@PathVariable("id") int id,
                                   @RequestBody Course course){
        try{
            courseService.save(course,id);
            return JsonResult.success();
        }catch(Exception e){
            return JsonResult.fail(e.getMessage());
        }
//        return JsonResult.success();
    }

    @DeleteMapping("/course/{id}")
    public JsonResult delete(@PathVariable("id") int id){
        courseService.delete(id);
        return JsonResult.success();
    }

    @PostMapping("/course/{id}/addLesson")
    public JsonResult addLesson(@PathVariable("id") int id,
                                @RequestBody List<Lesson> lessons
                                ){
        List<Lesson> lessonList = lessons.stream().peek(l -> l.setCourseId(id)).collect(Collectors.toList());
        courseService.addLessons(lessonList,id);
        return JsonResult.success();
    }

    @PutMapping("/course/{courseId}/lesson/{lessonId}")
    public JsonResult updateLesson (@RequestBody Lesson lesson){
        courseService.updateLesson(lesson,lesson.getCourseId());
        return JsonResult.success();
    }

    @DeleteMapping("/course/{courseId}/lesson/{lessonId}")
    public JsonResult updateLesson (@PathVariable("courseId") int courseId,@PathVariable("lessonId") int lessonId){
        courseService.deleteLesson(lessonId,courseId);
        return JsonResult.success();
    }

    @GetMapping("/course/student")
    public JsonResult getStudentList(@RequestParam("page") int page,
                                     @RequestParam("size") int size,
                                     @RequestParam("courseId") int courseId){
        Page<UserCourse> students = courseService.getCourseStudents(page, size, courseId);
        return JsonResult.success(students);
    }


}
